package mx.kolobok.noteeditor.ui.list;

import mx.kolobok.noteeditor.Note;
import mx.kolobok.noteeditor.ui.Icons;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: Nikita Belenkiy
 * Date: 30.01.11
 * Time: 23:56
 */
class NoteRenderer extends DefaultListCellRenderer {

    protected int draggingOver = -1;
    protected int mouseOverDelete = -1;

    private boolean isTargetCell = false;

    @Override
    public Component getListCellRendererComponent(JList list, @NotNull Object value, int index, boolean isSelected, boolean cellHasFocus) {

        String title = ((Note) value).getTitle();
        NoteList noteList = (NoteList) list;
        JLabel renderer = (JLabel) super.getListCellRendererComponent(noteList, title, index, isSelected, cellHasFocus);
        isTargetCell = index == draggingOver;

        addDeleteIcon(list, title, renderer,(index==mouseOverDelete&&draggingOver==-1)? Icons.deleteIconMouseOver: Icons.deleteIconDefault);
        return renderer;
    }

    private void addDeleteIcon(JList list, String title, JLabel renderer, Icon icon) {
        renderer.setIcon(icon);
        renderer.setHorizontalTextPosition(SwingConstants.LEFT);
        int titleWidth = SwingUtilities.computeStringWidth(renderer.getFontMetrics(renderer.getFont()), title);
        Insets borderInsets = renderer.getBorder().getBorderInsets(renderer);
        int rendererBorderWidth = borderInsets == null ? 0 : borderInsets.left + borderInsets.right;
        int iconTextGap = list.getWidth() - titleWidth - icon.getIconWidth() - rendererBorderWidth;
        renderer.setIconTextGap(iconTextGap);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (isTargetCell) {
            g.setColor(Color.blue);
            g.drawLine(0, 0, getSize().width, 0);
        }
    }
}
